package com.wtwd.campus.jdzc.controller;

import com.wtwd.campus.entity.FenceInfo;
import com.wtwd.campus.jdzc.common.Constant;
import com.wtwd.campus.jdzc.common.RespCode;
import com.wtwd.campus.jdzc.common.RespEntity;
import com.wtwd.campus.jdzc.service.FenceService;
import com.wtwd.campus.jdzc.service.ValidService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: ldaoliang
 * @DateTime: 2022/2/14 16:32
 * @Description:
 * 围栏管理需要重新建表，字段内容一样
 * 做围栏管理处理的时候针对交大众创的组织查询该表的数据
 */
@RestController
@RequestMapping("/jdzc/fence")
public class JdzcFenceController {

    private static final Logger logger = LoggerFactory.getLogger(JdzcFenceController.class);

    @Autowired
    private ValidService validService;
    @Autowired
    private FenceService fenceInfoService;

    /**
     * 获取围栏列表
     * @param studentId 学生id
     * @param request
     * @return
     */
    @GetMapping("/list/{stuId}")
    public RespEntity getFenceList(@PathVariable("stuId")Integer studentId, HttpServletRequest request){
        logger.info("获取学生的围栏列表： studentId = {}",studentId);
        String token = request.getHeader(Constant.HEADER_TOKEN_KEY);
        boolean isCorrespond = validService.validStudentAndTokenCorrespond(studentId,token);
        if(!isCorrespond){
            return new RespEntity(RespCode.ILLEGAL_OPERATION);
        }
        List<FenceInfo> fenceList = fenceInfoService.getJdzcFenceInfoListByStuId(studentId);
        return new RespEntity(RespCode.SUCCESS,fenceList);
    }

    /**
     * 添加围栏
     * @param fenceInfo
     * @param request
     * @return
     */
    @PostMapping("/add")
    public RespEntity addFence(@RequestBody FenceInfo fenceInfo, HttpServletRequest request){
        logger.info("添加学生围栏： fenceInfo = {}",fenceInfo);
        Integer studentId = fenceInfo.getStudent_id();
        String token = request.getHeader(Constant.HEADER_TOKEN_KEY);
        boolean isCorrespond = validService.validStudentAndTokenCorrespond(studentId,token);
        if(!isCorrespond){
            return new RespEntity(RespCode.ILLEGAL_OPERATION);
        }
        // 判断名称是否重复
        boolean fenceNameRepeat = fenceInfoService.fenceNameIsRepeat(fenceInfo.getFence_name(),studentId);
        if(fenceNameRepeat){
            return new RespEntity(RespCode.FENCE_NAME_REPEAT);
        }
        // 添加围栏
        Integer i = fenceInfoService.addJdzcWxFence(fenceInfo);
        return new RespEntity(i > 0 ? RespCode.SUCCESS : RespCode.FAILED);
    }

    /**
     * 删除围栏
     * @param fenceId
     * @param request
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public RespEntity delFence(@PathVariable("id")Integer fenceId,HttpServletRequest request){
        logger.info("删除围栏： id = {}",fenceId);
        FenceInfo fenceInfo = fenceInfoService.getFenceInfoByFenceId(fenceId);
        if(fenceInfo == null){
            return new RespEntity(RespCode.FENCE_NAME_REPEAT);
        }
        Integer studentId = fenceInfo.getStudent_id();
        String token = request.getHeader(Constant.HEADER_TOKEN_KEY);
        boolean isCorrespond = validService.validStudentAndTokenCorrespond(studentId,token);
        if(!isCorrespond){
            return new RespEntity(RespCode.ILLEGAL_OPERATION);
        }
        Integer i = fenceInfoService.deleteFenceInfo(fenceId);
        return new RespEntity(i > 0 ? RespCode.SUCCESS : RespCode.FAILED);
    }

    /**
     * 修改围栏信息
     * @param fenceInfo
     * @param request
     * @return
     */
    @PutMapping("/update")
    public RespEntity updateFence(@RequestBody FenceInfo fenceInfo, HttpServletRequest request){
        Integer fenceId = fenceInfo.getFence_id();
        if(fenceId == null){
            return new RespEntity(RespCode.PARAMETER_MISSING);
        }
        FenceInfo dataBaseFence = fenceInfoService.getFenceInfoByFenceId(fenceId);
        if(dataBaseFence == null){
            return new RespEntity(RespCode.FENCE_NAME_REPEAT);
        }
        Integer studentId = fenceInfo.getStudent_id();
        String token = request.getHeader(Constant.HEADER_TOKEN_KEY);
        boolean isCorrespond = validService.validStudentAndTokenCorrespond(studentId,token);
        if(!isCorrespond){
            return new RespEntity(RespCode.ILLEGAL_OPERATION);
        }
        Integer i = fenceInfoService.modifyFenceInfo(fenceInfo);
        return new RespEntity(i > 0 ? RespCode.SUCCESS : RespCode.FAILED);
    }





}
